// native method

function _call(funcName,params,cb){
    var rdm = Math.random().toString().substr(2),
        globalFuncName = "globalCallback" + rdm,
        iframeName = "nativeFuncIframe" + rdm,
        strParams = "";

        // createDocumentFragment
    let iframe = document.createElement('iframe')
    document.body.appendChild(iframe)

    window[globalFuncName] = function() {
        if (typeof callback === "function")
            callback.apply(this, arguments);
        delete window[globalFuncName];
        // $("#" + iframeName).remove();
        iframe.remove()
    }

    if (params && typeof params === "object") {
        params["callback"] = "window." + globalFuncName;
    } else {
        params = {
            callback: "window." + globalFuncName
        };
    }

    try {
        strParams = JSON.stringify(params);
    } catch (e) {
        strParams = '{"error":"params data error!"}';
    }

    iframe.setAttribute('src','callnative://' + funcName + '?' + encodeURIComponent(strParams))
}

let _tokenData = {
}

let P = {
    // 获取设备信息，设备ID
    getDeviceInfo:() =>{
        return new Promise((resolve,reject) => {
            _call('getDeviceInfo',null,(data) =>{
                if(data && data.deviceId !=''){
                    resolve(data)
                }else{
                    reject(data || {})
                }
            })
        })
    },
    // 复制到剪切板
    copyDataToClipboard:str =>{
        _call('copyDataToClipboard',str)
    }
}

window.P = P

export default P
